/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// cl_main.c  -- client main loop

#include "quakedef.h"

// we need to declare some mouse variables here, because the menu system
// references them even when on a unix system.

// these two are not intended to be set directly
cvar_t *cl_name;
cvar_t *cl_color;
cvar_t *cl_shownet;
cvar_t *cl_nolerp;
cvar_t *lookspring;
cvar_t *lookstrafe;
cvar_t *sensitivity;
cvar_t *m_pitch;
cvar_t *m_yaw;
cvar_t *m_forward;
cvar_t *m_side;


client_static_t	cls;
client_state_t	cl;

// FIXME: put these on hunk?
lightstyle_t	cl_lightstyle[MAX_LIGHTSTYLES];
dlight_t		cl_dlights[MAX_DLIGHTS];

int				cl_numvisedicts;
entity_t		*cl_visedicts[MAX_EDICTS];

/*
=====================
CL_ClearState

=====================
*/
void CL_ClearState (void)
{
	if (!sv.active)
		Host_ClearMemory ();

	// wipe the entire cl structure
	memset (&cl, 0, sizeof (cl));

	// wipe time
	cl.time = 0.0;

	SZ_Clear (&cls.message);

	// clear other arrays
	memset (cl_dlights, 0, sizeof (cl_dlights));
	memset (cl_lightstyle, 0, sizeof (cl_lightstyle));
}


/*
=====================
CL_Disconnect

Sends a disconnect message to the server
This is also called on Host_Error, so it shouldn't cause any errors
=====================
*/
void CL_Disconnect (void)
{
	// stop sounds (especially looping!)
	S_StopAllSounds (TRUE);

	// if running a local server, shut it down
	if (cls.demoplayback)
		CL_StopPlayback ();
	else if (cls.state == ca_connected)
	{
		if (cls.demorecording)
			CL_Stop_f ();

		Con_Printf (PRINT_DEVELOPER, "Sending clc_disconnect\n");
		SZ_Clear (&cls.message);
		MSG_WriteByte (&cls.message, clc_disconnect);
		NET_SendUnreliableMessage (cls.netcon, &cls.message);
		SZ_Clear (&cls.message);
		NET_Close (cls.netcon);

		cls.state = ca_disconnected;

		if (sv.active)
			Host_ShutdownServer (FALSE);
	}

	cls.demoplayback = cls.timedemo = FALSE;
	cls.signon = 0;
	cl.intermission = 0; // Baker: So critical.  SCR_UpdateScreen uses this.
}

void CL_Disconnect_f (void)
{
	CL_Disconnect ();

	if (sv.active)
		Host_ShutdownServer (FALSE);
}




/*
=====================
CL_EstablishConnection

Host should be either "local" or a net address to be passed on
=====================
*/
void CL_EstablishConnection (char *host)
{
	if (cls.demoplayback)
		return;

	CL_Disconnect ();

	cls.netcon = NET_Connect (host);

	if (!cls.netcon)
		Host_Error ("CL_Connect: connect failed\n");

	Con_Printf (PRINT_DEVELOPER, "CL_EstablishConnection: connected to %s\n", host);

	cls.demonum = -1;			// not in the demo loop now
	cls.state = ca_connected;
	cls.signon = 0;				// need all the signon messages before playing

	MSG_WriteByte (&cls.message, clc_nop); // ProQuake NAT Fix
}

/*
=====================
CL_SignonReply

An svc_signonnum has been received, perform a client side setup
=====================
*/
void CL_SignonReply (void)
{
	Con_Printf (PRINT_DEVELOPER, "CL_SignonReply: %i\n", cls.signon);

	switch (cls.signon)
	{
	case 1:
		MSG_WriteByte (&cls.message, clc_stringcmd);
		MSG_WriteString (&cls.message, "prespawn");
		break;

	case 2:
		MSG_WriteByte (&cls.message, clc_stringcmd);
		MSG_WriteString (&cls.message, va ("name \"%s\"\n", cl_name->string));

		MSG_WriteByte (&cls.message, clc_stringcmd);
		MSG_WriteString (&cls.message, va ("color %i %i\n", ((int) cl_color->value) >> 4, ((int) cl_color->value) & 15));

		MSG_WriteByte (&cls.message, clc_stringcmd);
		MSG_WriteString (&cls.message, va ("spawn %s", cls.spawnparms));
		break;

	case 3:
		MSG_WriteByte (&cls.message, clc_stringcmd);
		MSG_WriteString (&cls.message, "begin");
		break;

	case 4:
		SCR_EndLoadingPlaque ();		// allow normal screen updates
		break;
	}
}

/*
=====================
CL_NextDemo

Called to play the next demo in the demo loop
=====================
*/
void CL_NextDemo (void)
{
	if (cls.demonum == -1)
		return;		// don't play demos

	SCR_BeginLoadingPlaque ();

	if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
	{
		cls.demonum = 0;

		if (!cls.demos[cls.demonum][0])
		{
			Con_Printf (PRINT_DEFAULT, "No demos listed with startdemos\n");
			cls.demonum = -1;
			return;
		}
	}

	Cbuf_InsertText (va ("playdemo %s\n", cls.demos[cls.demonum]));
	cls.demonum++;
}

/*
==============
CL_PrintEntities_f
==============
*/
void CL_PrintEntities_f (void)
{
	entity_t	*ent;
	int			i;

	for (i = 0; i < cl.num_entities; i++)
	{
		ent = cl.entities[i];

		Con_Printf (PRINT_DEFAULT, "%3i:", i);

		if (!ent->model)
		{
			Con_Printf (PRINT_DEFAULT, "EMPTY\n");
			continue;
		}

		Con_Printf (
			PRINT_SAFE,
			"%s:%2i  (%5.1f,%5.1f,%5.1f) [%5.1f %5.1f %5.1f]\n",
			ent->model->name,
			ent->frame,
			ent->origin[0], ent->origin[1], ent->origin[2],
			ent->angles[0], ent->angles[1], ent->angles[2]
		);
	}
}


/*
===============
SetPal

Debugging tool, just flashes the screen
===============
*/
void SetPal (int i)
{
}


BOOL dlight_t::Alive (void)
{
	// light is already dead
	if (this->die < cl.time) return FALSE;

	// only lights with radius > 0 need apply
	if (this->radius > 0)
		return TRUE;
	else return FALSE;
}


void dlight_t::Initialize (int key, float radius, float die, float r, float g, float b)
{
	// default props may be overwritten post-alloc
	this->radius = radius;
	this->baseradius = radius;
	this->key = key;
	this->basetime = cl.time;
	this->die = cl.time + die;
	this->minlight = 0;
	this->decay = 0;

	Vector3Set (this->color, r, g, b);
	Vector3Set (this->angles, 0, 0, 0);

	this->ambientscale = 0.0f;
	this->diffusescale = 1.0f / 127.5f;

	this->flags = 0;
}


/*
===============
CL_AllocDlight

===============
*/
dlight_t *CL_AllocDlight (int key, float radius, float die, float r, float g, float b)
{
	int	i;
	dlight_t *dl;
	dlight_t *oldest = NULL;

	// first look for an exact key match
	if (key)
	{
		for (i = 0, dl = cl_dlights; i < MAX_DLIGHTS; i++, dl++)
		{
			if (dl->key == key)
			{
				dl->Initialize (key, radius, die, r, g, b);
				return dl;
			}
		}
	}

	// then look for a free light
	for (i = 0, dl = cl_dlights; i < MAX_DLIGHTS; i++, dl++)
	{
		// find the oldest dlight
		if (!oldest)
			oldest = dl;
		else if (dl->die < oldest->die)
			oldest = dl;

		// any dead light is good to use
		if (!dl->Alive ())
		{
			dl->Initialize (key, radius, die, r, g, b);
			return dl;
		}
	}

	// override the oldest dlight (which will be that with the lowest die value)
	if (oldest)
	{
		oldest->Initialize (key, radius, die, r, g, b);
		return oldest;
	}

	// just override the first one
	cl_dlights[0].Initialize (key, radius, die, r, g, b);
	return &cl_dlights[0];
}


void dlight_t::Decay (void)
{
	// kill after 1 frame no matter how long
	if (this->flags & DLF_KILL)
	{
		this->die = -1;
		this->radius = -1;
	}
	else this->radius = this->baseradius - ((cl.time - this->basetime) * this->decay);

	if (this->radius <= this->minlight)
	{
		this->radius = 0;
		this->die = -1;
	}
}


/*
===============
CL_DecayLights

===============
*/
void CL_DecayLights (void)
{
	int			i;
	dlight_t	*dl;

	for (i = 0, dl = cl_dlights; i < MAX_DLIGHTS; i++, dl++)
	{
		// already dead
		if (!dl->Alive ()) continue;

		dl->Decay ();
	}
}


/*
===============
CL_LerpPoint

Determines the fraction between the last two messages that the objects
should be put at.
===============
*/
float CL_LerpPoint (void)
{
	float f = cl.mtime[0] - cl.mtime[1];

	// because we now get multiple client frames out of sync with server frames we must remove sv.active from this condition
	if (!f || cl_nolerp->value || cls.timedemo)// || sv.active)
	{
		cl.time = cl.mtime[0];
		return 1;
	}

	if (f > 0.1f)
	{
		// dropped packet, or start of demo
		cl.mtime[1] = cl.mtime[0] - 0.1;
		f = 0.1f;
	}

	float frac = (cl.time - cl.mtime[1]) / f;

	//Con_Printf (PRINT_DEFAULT, "frac: %f\n",frac);
	if (frac < 0)
	{
		if (frac < -0.01)
		{
			SetPal (1);
			cl.time = cl.mtime[1];
			//				Con_Printf (PRINT_DEFAULT, "low frac\n");
		}

		frac = 0;
	}
	else if (frac > 1)
	{
		if (frac > 1.01f)
		{
			SetPal (2);
			cl.time = cl.mtime[0];
			//				Con_Printf (PRINT_DEFAULT, "high frac\n");
		}

		frac = 1;
	}
	else
		SetPal (0);

	return frac;
}


// update trails at half the standard FPS
static const double cl_traildelta = (2.0 / HOST_STANDARDFPS);

void CL_RocketTrail (entity_t *ent, int type)
{
	if (cl.time > ent->nexttrailtime)
	{
		R_RocketTrail (ent->oldtrailorigin, ent->origin, type);
		Vector3Copy (ent->oldtrailorigin, ent->origin);
		//ent->nexttrailtime += cl_traildelta;
		ent->nexttrailtime = cl.time + cl_traildelta;
	}
}


void CL_ClearRocketTrail (entity_t *ent)
{
	Vector3Copy (ent->oldtrailorigin, ent->origin);
	ent->nexttrailtime = cl.time + cl_traildelta;
}


void CL_LerpEntityAxis (entity_t *ent, float frac, int axis, BOOL steplerp)
{
	float delta = ent->msg_origins[0][axis] - ent->msg_origins[1][axis];

	if (delta > 100 || delta < -100)
	{
		// assume a teleportation, not a motion
		ent->origin[axis] = ent->msg_origins[0][axis];
		ent->angles[axis] = ent->msg_angles[0][axis];
		ent->lerpflags |= LERP_RESETMOVE;
	}
	else if (ent->lerpflags & LERP_MOVESTEP)
	{
		if (steplerp)
			ent->origin[axis] = ent->msg_origins[0][axis];
		else ent->origin[axis] = ent->msg_origins[1][axis] + frac * delta;

		ent->angles[axis] = ent->msg_angles[0][axis];
	}
	else
	{
		ent->origin[axis] = ent->msg_origins[1][axis] + frac * delta;
		ent->angles[axis] = ent->msg_angles[1][axis] + RAD2DEG (asin (sin (DEG2RAD (ent->msg_angles[0][axis] - ent->msg_angles[1][axis])) * frac));
	}
}


/*
===============
CL_RelinkEntities
===============
*/
void CL_RelinkEntities (void)
{
	entity_t	*ent;
	int			i;
	float		frac;
	float		bobjrotate;
	dlight_t	*dl;

	// determine partial update time
	frac = CL_LerpPoint ();

	cl_numvisedicts = 0;

	// interpolate player info
	for (i = 0; i < 3; i++)
		cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);

	if (cls.demoplayback)
	{
		// interpolate the angles
		cl.viewangles[0] = cl.mviewangles[1][0] + RAD2DEG (asin (sin (DEG2RAD (cl.mviewangles[0][0] - cl.mviewangles[1][0])) * frac));
		cl.viewangles[1] = cl.mviewangles[1][1] + RAD2DEG (asin (sin (DEG2RAD (cl.mviewangles[0][1] - cl.mviewangles[1][1])) * frac));
		cl.viewangles[2] = cl.mviewangles[1][2] + RAD2DEG (asin (sin (DEG2RAD (cl.mviewangles[0][2] - cl.mviewangles[1][2])) * frac));
	}

	bobjrotate = anglemod (100 * cl.time);

	// start on the entity after the world
	for (i = 1; i < cl.num_entities; i++)
	{
		ent = cl.entities[i];

		// if the object wasn't included in the last packet, remove it (fall through)
		if (ent->msgtime != cl.mtime[0])
		{
			CL_ClearRocketTrail (ent);
			ent->lerpflags |= LERP_RESETALL;
			ent->model = NULL;
		}

		// only static entities ever have efrags in GL
		if (!ent->model) continue;

		if (ent->forcelink)
		{
			// the entity was not updated in the last message so move to the final spot
			VectorCopy (ent->msg_origins[0], ent->origin);
			VectorCopy (ent->msg_angles[0], ent->angles);
			ent->lerpflags |= LERP_RESETALL;
			CL_ClearRocketTrail (ent);
		}
		else
		{
			// if LERP_STEPZ is set we allow lerping around the z axis in the renderer but we don't do so here
			// if LERP_STEPZ is cleared we allow lerping around the z axis here but we don't do so in the renderer
			// if the entity is not LERP_MOVESTEP it always lerps here but not in the renderer, irrespective of LERP_STEPZ
			if (ent->lerpflags & LERP_MOVESTEP)
			{
				if (ent->msg_origins[0][0] != ent->msg_origins[1][0] || ent->msg_origins[0][1] != ent->msg_origins[1][1])
					ent->lerpflags |= LERP_STEPZ;
				else if (ent->msg_origins[0][2] != ent->msg_origins[1][2])
					ent->lerpflags &= ~LERP_STEPZ;
				else ent->lerpflags |= LERP_STEPZ;
			}

			// interpolate the origin and angles
			CL_LerpEntityAxis (ent, frac, 0, TRUE);
			CL_LerpEntityAxis (ent, frac, 1, TRUE);
			CL_LerpEntityAxis (ent, frac, 2, ent->lerpflags & LERP_STEPZ);
		}

		// rotate binary objects locally
		if (ent->model->flags & EF_ROTATE)
		{
			ent->angles[0] = 0;
			ent->angles[1] = bobjrotate;
			ent->angles[2] = 0;
		}

		if (ent->effects & EF_BRIGHTFIELD)
			R_EntityParticles (ent);

		if (ent->effects & EF_MUZZLEFLASH)
		{
			vec3_t	fv, rv, uv;

			// because entity num is used as the key, because cl.viewentity never changes, and because a duplicate key just updates the current
			// light (instead of spawning a new one), this is safe for muzzleflashes
			if (i == cl.viewentity)
			{
				// switch the flash colour for the current weapon
				if (cl.stats[STAT_ACTIVEWEAPON] == IT_SUPER_LIGHTNING)
					dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_BLUE);
				else if (cl.stats[STAT_ACTIVEWEAPON] == IT_LIGHTNING)
					dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_BLUE);
				else dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_ORANGE);
			}
			else if (ent->model->flags & EF_WIZARDFLASH)
				dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_GREEN);
			else if (ent->model->flags & EF_SHALRATHFLASH)
				dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_PURPLE);
			else if (ent->model->flags & EF_SHAMBLERFLASH)
				dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_BLUE);
			else if (ent->model->flags & EF_ORANGEFLASH)
				dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_ORANGE);
			else if (ent->model->flags & EF_REDFLASH)
				dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_RED);
			else if (ent->model->flags & EF_YELLOWFLASH)
				dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_YELLOW);
			else dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 0.1f, DL_COLOR_ORANGE);

			Vector3Copy (dl->origin, ent->origin);
			dl->origin[2] += 16;
			dl->minlight = 32;
			AngleVectors (ent->angles, fv, rv, uv);

			Vector3Mad (dl->origin, fv, 18, dl->origin);

			// clear the flash client-side too
			ent->effects &= ~EF_MUZZLEFLASH;

			if (ent == cl.entities[cl.viewentity])
				cl.viewent.lerpflags |= LERP_RESETANIM | LERP_RESETANIM2;
			else ent->lerpflags |= LERP_RESETANIM;
		}

		if (ent->effects & EF_BRIGHTLIGHT)
		{
			dl = CL_AllocDlight (i, 400 + (CL_TimedRandom (i, 36) & 31), 666, DL_COLOR_WHITE);
			Vector3Copy (dl->origin, ent->origin);
			dl->origin[2] += 16;
			dl->flags = DLF_KILL;
		}

		if (ent->effects & EF_DIMLIGHT)
		{
			// powerup dynamic lights
			if (i == cl.viewentity)
			{
				// and set the appropriate colour depending on the current powerup(s)
				if ((cl.items & IT_QUAD) && (cl.items & IT_PENT))
					dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 666, DL_COLOR_PURPLE);
				else if (cl.items & IT_QUAD)
					dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 666, DL_COLOR_BLUE);
				else if (cl.items & IT_PENT)
					dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 666, DL_COLOR_RED);
				else dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 666, DL_COLOR_WHITE);
			}
			else dl = CL_AllocDlight (i, 200 + (CL_TimedRandom (i, 36) & 31), 666, DL_COLOR_WHITE);

			Vector3Copy (dl->origin, ent->origin);
			dl->flags = DLF_KILL;
		}

		if (ent->model->flags & EF_GIB)
			CL_RocketTrail (ent, RT_BLOOD);
		else if (ent->model->flags & EF_ZOMGIB)
			CL_RocketTrail (ent, RT_SLIGHTBLOOD);
		else if (ent->model->flags & EF_WIZARDTRAIL)
		{
			// wizard
			CL_RocketTrail (ent, RT_TRACERWIZARD);
			dl = CL_AllocDlight (i, 200, 666, DL_COLOR_GREEN);
			dl->flags = DLF_KILL;
			Vector3Copy (dl->origin, ent->origin);
		}
		else if (ent->model->flags & EF_KNIGGITTRAIL)
		{
			// kniggit
			CL_RocketTrail (ent, RT_TRACERKNIGGIT);
			dl = CL_AllocDlight (i, 200, 666, DL_COLOR_ORANGE);
			dl->flags = DLF_KILL;
			Vector3Copy (dl->origin, ent->origin);
		}
		else if (ent->model->flags & EF_ROCKET)
		{
			// boom boom
			CL_RocketTrail (ent, RT_ROCKETTRAIL);
			dl = CL_AllocDlight (i, 200, 666, DL_COLOR_ORANGE);
			dl->flags = DLF_KILL;
			Vector3Copy (dl->origin, ent->origin);
		}
		else if (ent->model->flags & EF_GRENADE)
			CL_RocketTrail (ent, RT_SMOKETRAIL);
		else if (ent->model->flags & EF_VORETRAIL)
		{
			// voor
			CL_RocketTrail (ent, RT_TRACERVORE);
			dl = CL_AllocDlight (i, 200, 666, DL_COLOR_PURPLE);
			dl->flags = DLF_KILL;
			Vector3Copy (dl->origin, ent->origin);
		}

		ent->forcelink = FALSE;

		// the view entity is updated for position and effects but not added to the draw lists (this is an FPS after all)
		if (i == cl.viewentity)
			continue;

		if (cl_numvisedicts < MAX_EDICTS)
		{
			cl_visedicts[cl_numvisedicts] = ent;
			cl_numvisedicts++;
		}
		else Con_Printf (PRINT_SAFE, "cl_numvisedicts >= MAX_EDICTS\n");
	}
}


/*
===============
CL_ReadFromServer

Read all incoming data from the server
===============
*/
void CL_ReadFromServer (double frametime)
{
	int ret;

	if (cls.state == ca_connected)
	{
		// advance time
		cl.time += frametime;

		do
		{
			ret = CL_GetMessage ();

			if (ret == -1)
				Host_Error ("CL_ReadFromServer: lost server connection");

			if (!ret)
				break;

			CL_ParseServerMessage ();
		} while (ret && cls.state == ca_connected);

		if (cl_shownet->value)
			Con_Printf (PRINT_DEFAULT, "\n");
	}
}


/*
=================
CL_SendCmd
=================
*/
void CL_SendCmd (double frametime)
{
	usercmd_t		cmd;

	if (cls.state != ca_connected)
		return;

	if (cls.signon == SIGNONS)
	{
		// get basic movement from keyboard
		CL_BaseMove (frametime, &cmd);

		// send the unreliable message
		CL_SendMove (&cmd);
	}

	if (cls.demoplayback)
	{
		SZ_Clear (&cls.message);
		return;
	}

	// send the reliable message
	if (!cls.message.cursize)
		return;		// no message at all

	if (!NET_CanSendMessage (cls.netcon))
	{
		Con_Printf (PRINT_DEVELOPER, "CL_SendCmd: can't send\n");
		return;
	}

	if (NET_SendMessage (cls.netcon, &cls.message) == -1)
		Host_Error ("CL_SendCmd: lost server connection");

	SZ_Clear (&cls.message);
}

/*
=================
CL_Init
=================
*/
void CL_Init (void)
{
	SZ_Alloc (&cls.message, 1024);

	CL_InitEffects ();
	CL_InitInput ();

	// register our commands
	cl_name = Cvar_Get ("_cl_name", "player", CVAR_ARCHIVE);
	cl_color = Cvar_Get ("_cl_color", "0", CVAR_ARCHIVE);
	cl_shownet = Cvar_Get ("cl_shownet", "0");
	cl_nolerp = Cvar_Get ("cl_nolerp", "0");

	lookspring = Cvar_Get ("lookspring", "0", CVAR_ARCHIVE);
	lookstrafe = Cvar_Get ("lookstrafe", "0", CVAR_ARCHIVE);
	sensitivity = Cvar_Get ("sensitivity", "3", CVAR_ARCHIVE);

	m_pitch = Cvar_Get ("m_pitch", "0.022", CVAR_ARCHIVE);
	m_yaw = Cvar_Get ("m_yaw", "0.022", CVAR_ARCHIVE);
	m_forward = Cvar_Get ("m_forward", "1", CVAR_ARCHIVE);
	m_side = Cvar_Get ("m_side", "0.8", CVAR_ARCHIVE);

	cl_upspeed = Cvar_Get ("cl_upspeed", "200");
	cl_forwardspeed = Cvar_Get ("cl_forwardspeed", "200", CVAR_ARCHIVE);
	cl_backspeed = Cvar_Get ("cl_backspeed", "200", CVAR_ARCHIVE);
	cl_sidespeed = Cvar_Get ("cl_sidespeed", "350");

	cl_movespeedkey = Cvar_Get ("cl_movespeedkey", "2");
	cl_yawspeed = Cvar_Get ("cl_yawspeed", "140");
	cl_pitchspeed = Cvar_Get ("cl_pitchspeed", "150");
	cl_anglespeedkey = Cvar_Get ("cl_anglespeedkey", "1.5");

	Cmd_AddCommand ("entities", CL_PrintEntities_f);
	Cmd_AddCommand ("disconnect", CL_Disconnect_f);
	Cmd_AddCommand ("record", CL_Record_f);
	Cmd_AddCommand ("stop", CL_Stop_f);
	Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
	Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
}

